package com.yunc.modules.shop.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.aspect.DataSource;
import com.yunc.base.controller.ShopBaseController;
import com.yunc.base.aspect.DataSourceType;
import com.yunc.base.utils.R;
import com.yunc.modules.shop.entity.ShopBanner;
import com.yunc.modules.shop.service.IShopBannerService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * banner轮播图
 *
 * @author congcong tao
 * @date 2018/12/24 9:27
 */
@RestController
@RequestMapping("shopBanner")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopBannerController extends ShopBaseController {

    private final IShopBannerService shopBannerService;

    @GetMapping("/page")
    @DataSource(value = DataSourceType.SHOP)
    public R page(Page<ShopBanner> page, ShopBanner banner){
        QueryWrapper<ShopBanner> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(banner.getScopeType())) {
            qw.eq("scope_type", banner.getScopeType());
        }
        setShop(qw);
        shopBannerService.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    @DataSource(value = DataSourceType.SHOP)
    public R list(ShopBanner banner){
        QueryWrapper<ShopBanner> ew = new QueryWrapper<>();
        List<ShopBanner> list = shopBannerService.list(ew);
        return R.ok().put("list", list).put("banner", banner);
    }

    @GetMapping("/info/{id}")
    @DataSource(value = DataSourceType.SHOP)
    public R info(@PathVariable String id){
        ShopBanner banner = shopBannerService.getById(id);
        return R.ok().put("banner", banner);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @DataSource(value = DataSourceType.SHOP)
    public R save(ShopBanner banner){
        setShopId(banner);
        banner.setCreateTime(new Date());
        boolean flag = shopBannerService.save(banner);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    @DataSource(value = DataSourceType.SHOP)
    public R update(ShopBanner banner){
        boolean flag = shopBannerService.updateById(banner);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    @DataSource(value = DataSourceType.SHOP)
    public R delete(@PathVariable String ids){
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = shopBannerService.removeByIds(idsList);
        if (flag) {
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }
}
